{% extends 'configmanager/__base__.html' %}
{% block title %}Jenkins管理 | {% endblock %}
{% block search_notice %}请输入 Jenkins任务名称{% endblock %}
{% block content %}


    <div class="btn-toolbar container-fluid" role="toolbar">
        <div class="btn-group">
            <a href="javascript:void(0);" onclick="SyncAllJenkinsJob('{{ page_obj.number }}')" type="button"
               class="btn btn-success">同步所有 Jenkins 任务</a>
            <a href="javascript:void(0);" onclick="SyncAllJenkinsLastJobNumber('{{ page_obj.number }}')" type="button"
               class="btn btn-info">同步所有 Jenkins 任务的 LastSuccessBuildNum</a>
        </div>
    </div><br>

    <div id="whole" class="container-fluid table-responsive">

        {% if jenkins_job_list %}
            <table class="table table-striped table-hover table-condensed">
                <thead>
                <th>Jenkins 任务名称</th>
                <th>最后一次成功构建版本号</th>
                <th>操作</th>
                </thead>
                <tbody>
                {% for job in jenkins_job_list %}
                    <tr id="tr_{{ job.id }}">
                        <td>
                            {{ job.name }}
                        </td>
                        <td>
                            {{ job.last_success_num }}
                        </td>
                        <td>
                            <a href="javascript:void(0);"
                               onclick="UpdateLastSuccessBuildNum('{{ job.id }}', '{{ job.name }}')"
                               type="button" class="btn btn-info">更新最后一次成功构建版本号</a>
                            <a href="javascript:void(0);" onclick="Build('{{ job.id }}', '{{ job.name }}')"
                               type="button" class="btn btn-success">构建</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        {% else %}
            <p>No Jenkins Jobs are available.</p>
        {% endif %}
    </div>



    <script>


        $.ajaxSetup({
            data: {csrfmiddlewaretoken: '{{ csrf_token }}'},
        });

        //局部刷新
        function PartialRefresh(jobid) {
            var postUrl = "/jenkins/" + jobid + "/partrefresh/";
            $.post(postUrl, function (data) {
                $('#tr_' + jobid).html(data)
            });
        }

        //主体刷新
        function WholePartialRefresh(pagenumber) {
            var postUrl = "/jenkins_job_list/wholerefresh/" + pagenumber + "/";
            $.post(postUrl, function (data) {
                $('#whole').html(data)
            });
        }

        //同步所有 Jenkins 任务
        function SyncAllJenkinsJob(pagenumber) {
            var postUrl = "/jenkins/sync_job/";
            var pagenumber = pagenumber;

            jQuery('#body').showLoading();

            $.post(postUrl, function (data) {
                if (data.success) {
                    WholePartialRefresh(pagenumber);
                    $("#alert-div").css("display", "");
                    $("#alert-div").attr("class", "alert alert-success");
                    $("#alert-div").text("成功！同步所有 Jenkins 任务");
                    setTimeout(function () {
                        $("#alert-div").fadeOut();
                    }, 20000);
                } else {
                    alert("操作失败！" + data.message);
                }
                setTimeout(function () {
                    jQuery('#body').hideLoading();
                }, 300);
            });
        }


        //更新所有 Jenkins 任务的最后一次成功构建版本号
        function SyncAllJenkinsLastJobNumber(pagenumber) {
            var postUrl = "/jenkins/sync_job_last_success_build_num/";
            var pagenumber = pagenumber;

            jQuery('#body').showLoading();

            $.post(postUrl, function (data) {
                if (data.success) {
                    WholePartialRefresh(pagenumber);
                    $("#alert-div").css("display", "");
                    $("#alert-div").attr("class", "alert alert-success");
                    $("#alert-div").text("成功！同步所有 Jenkins 任务最后一次成功构建版本号");
                    setTimeout(function () {
                        $("#alert-div").fadeOut();
                    }, 20000);
                } else {
                    alert("操作失败！" + data.message);
                }
                setTimeout(function () {
                    jQuery('#body').hideLoading();
                }, 300);
            });
        }


        // 更新jenkins 最后一次成功构建版本号
        function UpdateLastSuccessBuildNum(jobid, jobname) {
            var postUrl = "/jenkins/" + jobid + "/update/";

            jQuery('#body').showLoading();

            $.post(postUrl, function (data) {
                if (data.success) {
                    PartialRefresh(jobid);
                    $("#alert-div").css("display", "");
                    $("#alert-div").attr("class", "alert alert-success");
                    $("#alert-div").text("成功！更新 Job " + jobname);
                    setTimeout(function () {
                        $("#alert-div").fadeOut();
                    }, 20000);
                } else {
                    alert("操作失败！" + data.message);
                }
                setTimeout(function () {
                    jQuery('#body').hideLoading();
                }, 300);
            });
        }


        // 构建 Jenkins 任务
        function Build(jobid, jobname) {
            var postUrl = "/jenkins/" + jobid + "/build/";

            jQuery('#body').showLoading();

            $.post(postUrl, function (data) {
                if (data.success) {
                    PartialRefresh(jobid);
                    $("#alert-div").css("display", "");
                    $("#alert-div").attr("class", "alert alert-success");
                    $("#alert-div").text("成功！触发构建 Job " + jobname);
                    setTimeout(function () {
                        $("#alert-div").fadeOut();
                    }, 20000);
                } else {
                    alert("操作失败！" + data.message);
                }
                setTimeout(function () {
                    jQuery('#body').hideLoading();
                }, 300);
            });
        }

    </script>

{% endblock %}




